package com.example.musicproject_b.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.b_pojo.BAdminUser;
import com.example.common.utils.Resultb;
import com.example.service.BAdminRoleService;
import com.example.service.BAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/B_admin/adminmannager")
public class AdminPermisionController {

    @Autowired
    private BAdminService bAdminService;

    @Autowired
    private BAdminRoleService bAdminRoleService;



    /**
     *  管理员列表(固定图片，不存在更改头像，不包含逻辑删除)
     * @return
     * @param @param null
     * @author znk
     * @date 2024/8/23 11:09
     */
    @RequestMapping("/adminalllist")
    public Resultb list(int pageNum, int pageSize) {
        Page<BAdminUser> alladmin = bAdminService.getAlladmin(pageNum, pageSize);
        if (alladmin.getRecords().size() > 0) {
            return Resultb.success(alladmin);
        }
        return Resultb.error();
    }

    /**
     *  管理员列表(包含逻辑删除)
     * @return
     * @param @param null
     * @author znk
     * @date 2024/8/23 11:09
     */


    /**
     * @return
     * @param @param null
     * @author znk
     * @date 2024/8/23 11:13
     * 删除启用禁用管理员
     */
//TODO 管理员id为4的管理员不能被删除，为超级管理员
    @RequestMapping("/stopadmin")
    public Resultb deleteadmin(String id) {
        if("4".equals(id)){
            return Resultb.error("超级管理员不能被删除!");
        }
        boolean i = bAdminService.updateIsdeleteAdmin(Long.parseLong(id));
        if (i) {
            return Resultb.success();
        }
        return Resultb.error();
    }

    /**
     * @return
     * @param @param null
     * @author znk
     * @date 2024/8/23 11:13
     * 修改管理员自己的信息(包含逻辑删除)
     */
    @RequestMapping("/updateadmin")
    public Resultb updateadmin(@RequestBody BAdminUser admin) {
        int i = bAdminService.updateAdmin(admin);
        if (i > 0) {
            return Resultb.success();
        }
        return Resultb.error();
    }

    /**
     * @return
     * @param @param null
     * @author znk
     * @date 2024/8/27 18:28
     * 为指定管理员添加角色
     */
    @RequestMapping("/addadminrole")
    public Resultb addadminrole(String adminid, String roleid) {
        boolean b = bAdminRoleService.insertAdminRole(Long.parseLong(adminid), Long.parseLong(roleid));
        if (b) {
            return Resultb.success();
        }
        return Resultb.error();
    }

    /**
     * @return
     * @param @param null
     * @author znk
     * @date 2024/8/28 13:46
     * 为指定管理员删除角色
     */
    @RequestMapping("/deleteadminrole")
    public Resultb deleteadminrole(String adminid,String roleid) {
        boolean b = bAdminRoleService.deleteAdminRole(Long.parseLong(adminid), Long.parseLong(roleid));
        if (b) {
            return Resultb.success();
        }
        return Resultb.error();
    }




}
